package com.douqu.game.core.config.sprite;


import com.douqu.game.core.config.battle.SkillConfig;
import com.douqu.game.core.config.common.CommonData;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.LoadFactory;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.core.util.CoreUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2017-10-16 16:55
 */
public class MasterConfig extends Sprite {

    /** 普通攻击技能 */
    public SkillConfig defaultSkill;


    /** 主将技能 id需要的主将等级， value 对应的skill id */
    public CommonData[] skills;



    public MasterConfig()
    {
        this.unitType = ConstantFactory.UNIT_TYPE_LAND;
    }

    @Override
    public boolean check()
    {
        if(defaultSkill == null)
        {
            LogUtils.error("Load Master defaultSkill is null! id:" + id);
            return false;
        }

        if(skills == null)
        {
            LogUtils.error("Load Master skills is null! id:" + id);
            return false;
        }

//        if(ai == null)
//        {
//            LogUtils.warn("AI暂时没有配置，写死为1");
//            ai = DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.AI_KEY, 1);
//            return true;
//        }

        return super.check();
    }

    public SkillConfig getDefaultSkill()
    {
        return defaultSkill;
    }

    public List<Integer> getSkills(int lv)
    {
        List<Integer> result = new ArrayList<>();
        for(CommonData data : skills)
        {
            if(data.id <= lv)
                result.add(data.value);
        }

        return result;
    }




    @Override
    public boolean setVariable(String key, String value)
    {
        if("skills".equals(key))
        {
            if(CoreUtils.isErrorValue(value))
            {
                skills = new CommonData[0];
            }
            else
            {
                skills = LoadUtils.loadCommonDataToArray(key, value);
                for(CommonData skill : skills)
                {
                    if(DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.SKILL_KEY, skill.value) == null)
                    {
                        LogUtils.error("Master setVariable Error -> skill : " + value + " id:" + id);
                        return false;
                    }
                    if(skill.id <= 0)
                    {
                        LogUtils.error("Master setVariable Error -> need level  error : " + value + " id:" + value + " id:" + id);
                        return false;
                    }
                }
            }
            return true;
        }
        else if("defaultSkill".equals(key))
        {
            defaultSkill = DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.SKILL_KEY, Integer.parseInt(value));
            if(defaultSkill == null)
            {
                LogUtils.error("Load Master Error -> defaultSkill is null:" + id);
                return false;
            }
            return true;
        }
//        else if("ai".equals(key))
//        {
//            ai = DataFactory.getInstance(LoadFactory.isTestLoad()).getGameObject(DataFactory.AI_KEY, Integer.parseInt(value));
//            if(ai == null)
//            {
//                LogUtils.error("Load Master Error -> ai is null:" + id);
//                return false;
//            }
//            return true;
//        }
        else
            return super.setVariable(key, value);
    }



}
